package giro.general.adapter;

import giro.general.R;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import android.content.Context;
import android.util.DisplayMetrics;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.WindowManager;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

public class SaliteDetailGalleryAdapter extends BaseAdapter {
	private Context mContext;
	private LayoutInflater mLayoutInflater;
	private JSONArray mStageArray;
	private int position;
	
	//private SaliteDetailGalleryAdapter mSaliteDetailGalleryAdapter;

	public SaliteDetailGalleryAdapter(Context context, JSONArray stageArray) {
		mContext = context;
		
		mLayoutInflater = (LayoutInflater) mContext
				.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
		DisplayMetrics metrics = new DisplayMetrics();
		((WindowManager) mContext.getSystemService(Context.WINDOW_SERVICE))
				.getDefaultDisplay().getMetrics(metrics);
		mStageArray = stageArray;
		// mPlaceholder = ((BitmapDrawable)
		// mContext.getResources().getDrawable(R.drawable.placeholder_img)).getBitmap();
	}

	public int getCount() {
		if (mStageArray != null) {
			return mStageArray.length();
		}
		return 0;
	}

	public Object getItem(int position) {

		if (mStageArray != null) {
			try {
				return mStageArray.get(position);
			} catch (JSONException e) {
				e.printStackTrace();
			}
		}
		return null;
	}

	public long getItemId(int position) {
		return position;
	}

	public View getView(int position, View convertView, ViewGroup parent) {
		this.position =position;
		LinearLayout view = (LinearLayout) convertView;

		if (view == null) {
			try{
			view = (LinearLayout) mLayoutInflater.inflate(
					R.layout.salite_gallery_item, null);
			}
			catch(Exception e){
				
				view = (LinearLayout) mLayoutInflater.inflate(
						R.layout.salite_gallery,null);
			}
		}
		
		
		
		JSONObject o = null;
		try {
			o = (JSONObject) mStageArray.get(position);
			
		} catch (JSONException e1) {
			e1.printStackTrace();
		}
		String fileSalita="";
		String nomeSalita="";
		
		try {
			fileSalita = o.getString("dafile");
			nomeSalita = o.getString("nome");
		} catch (JSONException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		
		int nameResourceID = mContext.getResources().getIdentifier(fileSalita,
				"drawable", mContext.getApplicationInfo().packageName);
		ImageView imageView = (ImageView)view.findViewById(R.id.salita_gallery_item_image);
		imageView.setImageResource(nameResourceID);
		
		TextView itemNumberText = (TextView) view.findViewById(R.id.salita_gallery_item__nome);
		itemNumberText.setText(nomeSalita);
		
		
		
		
		// TextView newPriceText = (TextView)
		// view.findViewById(R.id.promo_item_detail_product_price_text);
		// newPriceText.setText(promoItem.getNewPrice());
		//
		// TextView oldPriceText = (TextView)
		// view.findViewById(R.id.promo_item_detail_product_oldprice_text);
		// oldPriceText.setText(promoItem.getOldPrice());
		// oldPriceText.setPaintFlags(oldPriceText.getPaintFlags() |
		// Paint.STRIKE_THRU_TEXT_FLAG);
		//
		
		// ImageView promoTypeImage = (ImageView)
		// view.findViewById(R.id.promo_type_image);
		//
		// setImages(promoItem, promoImage, promoTypeImage);
		//
		// TextView prodDescriptionText = (TextView)
		// view.findViewById(R.id.promo_item_detail_product_detail_text);
		// prodDescriptionText.setText(promoItem.getMeasure());
		//
		// if (promoItem.getPromoType() == 2) {
		// ImageView fra = (ImageView)
		// view.findViewById(R.id.fragola_ico_image);
		// fra.setVisibility(View.VISIBLE);
		// }
		//
		// if (promoItem.getStartDate() != null && promoItem.getEndDate() !=
		// null) {
		// TextView datePeriodText = (TextView)
		// view.findViewById(R.id.promo_item_detail_pretty_date_period_text);
		//
		// String periodText = null;
		// try {
		// periodText = Utils.formatPromoPeriod(promoItem.getStartDate(),
		// promoItem.getEndDate(),1);
		// } catch (Exception pe) {
		// }
		//
		// datePeriodText.setText(periodText);
		// }
		//
		// try {
		// if (promoItem.hasFeatures()) {
		// LinearLayout container = (LinearLayout)
		// view.findViewById(R.id.item_features_container);
		// //container.removeAllViews();
		//
		// for (PromoItemFeature feature : promoItem.getPromoItemFeatures()) {
		// container.addView(getPromoItemFeatureImage(feature));
		// }
		// }
		// } catch (Exception e) {
		// //e.printStackTrace();
		// }

		return view;
	}

	public void getSalita() {
		// TODO Auto-generated method stub
		
	}



	// private Bitmap loadLocalImage(final String filename) {
	// Thread t = new Thread() {
	// @Override
	// public void run() {
	// File promoFile = mContext.getFileStreamPath(filename);
	// if (promoFile.exists()) {
	// try {
	// Bitmap bm = BitmapFactory.decodeStream(mContext.openFileInput(filename));
	// syncNotifyDataSetChanged();
	// } catch (FileNotFoundException e) {
	// // System.err.println("img prodotto [" +
	// // filename + "] non trovata: " +
	// // e.getMessage());
	// // promoImage.setImageResource(R.drawable.placeholder_img);
	// }
	// }
	//
	// }
	// };
	// t.start();
	//
	// return null;
	// }
	// }

	// public void recycleBitmaps() {
	// Collection<Bitmap> bitmaps =
	// ImageCache.getInstance().getmImageCache().values();
	//
	// Utils.hardLong(this.getClass().getSimpleName() + " has " + bitmaps.size()
	// + " image(s).");
	//
	// int recycled = 0;
	// for (Bitmap bitmap : bitmaps) {
	// if (bitmap != null && !bitmap.isRecycled()) {
	// recycled++;
	// bitmap.recycle();
	// }
	// }
	// Utils.hardLong(this.getClass().getSimpleName() + " has recycled " +
	// recycled + " image(s).");
	//
	// ImageCache.getInstance().getmImageCache().clear();
	//
	// /*
	// if (mPlaceholder != null && !mPlaceholder.isRecycled()) {
	// mPlaceholder.recycle();
	// }*/
	// }

}
